/*
 * @Author: wenqiang zhang
 * @Date: 2020-12-08 13:32:28
 * @LastEditTime: 2021-06-01 20:22:06
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: /bridge-tunnel-ui/src/permission.js
 */
import router from './router'
import store from './store'
import { Message } from 'element-ui'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
import { getToken, getCookie, removeCookie, setCookie } from '@/utils/auth'
import { LOGIN_TIME_OUT } from '@/utils/dict'
import Vue from 'vue'
import rest from '@/store/rest.js'

NProgress.configure({ showSpinner: false })

const whiteList = ['/login', '/auth-redirect', '/bind', '/register']
let timer
function getQueryVariable(variable) {
  var query = window.location.search.substring(1);
  var vars = query.split("&");
  for (var i=0;i<vars.length;i++) {
    var pair = vars[i].split("=");
    if(pair[0] == variable){return pair[1];}
  }
  return false;
}
router.beforeEach(async (to, from, next) => {
  NProgress.start()
  // window.localStorage.setItem('tOut', LOGIN_TIME_OUT)
  // if (getCookie('uid')) { // 登录
  //   if (to.path === '/main') { // 登录超时
  //     if (timer) {
  //       window.clearInterval(timer)
  //     }
  //     timer = window.setInterval(() => {
  //       let tOut = 0
  //       if (window.localStorage.getItem('tOut') || window.localStorage.getItem('tOut') == 0) {
  //         tOut = window.localStorage.getItem('tOut')
  //       }
  //       tOut--
  //       window.localStorage.setItem('tOut', tOut)
  //       if (window.localStorage.getItem('tOut') <= 0) {
  //         removeCookie('uid')
  //         removeCookie('token')
  //         next({ path: '/login' })
  //         const vue = new Vue()
  //         vue.msgInfo('登入已超时， 请重新登录。')
  //       }
  //     }, 1000)
  //   }
  //   if (to.path === '/login') {
  //     next({ path: '/' })
  //     NProgress.done()
  //   } else {
  //     next()
  //     NProgress.done()
  //   }
  // } else {
  //   // 没有token
  //   // if (whiteList.indexOf(to.path) !== -1) {
  //   //   // 在免登录白名单，直接进入
  //   //   next()
  //   // } else {
  //   //   next('/login') // 否则全部重定向到登录页
  //   //   NProgress.done()
  //   // }
  //   if (timer) { // 登录超时
  //     window.clearInterval(timer)
  //   }
  //   next()
  // }
  if (getCookie('openid')) {
    next();
  } else {
    // const state = '7cce2563381e2cdf9b775439a271833f';
    // if (!to.query.code && !getQueryVariable('code')) {
    //   window.location.href = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx3c14646ee2420a3f&redirect_uri=${encodeURIComponent('http://video.xgichina.net/')}&response_type=code&scope=snsapi_base&state=${state}#wechat_redirect`;
    // } else {
    //   if (to.query.state == state || getQueryVariable('state') == state) {
    //     const res = await rest.get('index/index/wx_callback', { code: to.query.code || getQueryVariable('code'), state: to.query.state || getQueryVariable('state') });
    //     setCookie('openid', res.data.openid)
    //     next({ 'path': '/main' });
    //   }
    // }
    setCookie('openid', 'odglT6GJE2ciZ4NZNKCxHU9euLCY')
    next({ 'path': '/main' });
    // if (to.path === '/main') {
    //   next({ 'path': '/auth' });
    // }
  }
  next();
})

router.afterEach(() => {
  NProgress.done()
})
